<?php


namespace app\controller\admin;


use app\common\Consts;
use app\model\admin\AdminRoleModel;
use app\validate\admin\RoleValidate;
use think\App;
use think\facade\Db;
use think\Request;

class RoleController extends BaseAdminController
{

    function setModel()
    {
        return new AdminRoleModel();
    }

    function setWithModel()
    {
        // TODO: Implement setWithModel() method.
    }

    function setSearch()
    {
        return [
            '=' => ['role_id']
        ];
    }

    function setSort()
    {
        // TODO: Implement setSort() method.
    }

    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 角色列表
     * @return \think\response\Json
     */
    public function getRoleList()
    {
        try {
            $userInfo = $this->request->middleware('userInfo');
            $roleId = $userInfo->role_id;
            $where = [];
            $where[] = ['is_del', '=', 0];
            if ($roleId != Consts::ADMIN_ROLE_ID) {
                $childRoleIdArr = get_child_role_id($roleId);
                $where[] = ['id', 'in', $childRoleIdArr];
            }
            $where = array_merge($this->searchWhere, $where);
            $list = DB::name('admin_role')
                ->where($where)
                ->order('id', 'desc')
                ->select()
                ->toArray();
            $list = list_to_tree($list, 'id', 'pid', 'children');
            foreach ($list as &$value) {
                $value['_showChildren'] = true;
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     *添加角色
     * @return \think\response\Json
     */
    public function add()
    {
        Db::startTrans();
        try {
            $data = $this->request->param('', '', 'trim');
            Validate(RoleValidate::class)->scene('add')->check($data);
            AdminRoleModel::create($data);
            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception);
        }
    }

    /**
     *获取自己和下级角色
     * @return \think\response\Json
     */
    public function getSelfAndNextRoleList()
    {
        try {
            $userInfo = $this->request->middleware('userInfo');
            $roleId = $userInfo->role_id;
            $where = [];
            $where[] = ['is_del', '=', 0];
            if ($roleId != Consts::ADMIN_ROLE_ID) {
                $roleIdArr = get_child_role_id($roleId);
                array_push($roleIdArr, $roleId);
                $where[] = ['id', 'in', $roleIdArr];
            }
            $list = DB::name('admin_role')
                ->where($where)
                ->select();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 获取下级角色
     * @return \think\response\Json
     */
    public function getNextRoleList()
    {
        try {
            $userInfo = $this->request->middleware('userInfo');
            $roleId = $userInfo->role_id;
            $where = [];
            $where[] = ['is_del', '=', 0];
            if ($roleId != Consts::ADMIN_ROLE_ID) {
                $roleIdArr = get_child_role_id($roleId);
                $where[] = ['id', 'in', $roleIdArr];
            }
            $list = DB::name('admin_role')
                ->where($where)
                ->order('id', 'asc')
                ->select();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 删除角色
     * @return \think\response\Json
     */
    public function del()
    {
        try {
            $id = $this->request->param('id', 0);
            $model = AdminRoleModel::find($id);
            if ($model->is_system) {
                throw new \Exception('系统角色不可删除');
            }
            $model->is_del = 1;
            $model->save();
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

}